﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZDSoft.LMS.Service;
using ZDSoft.LMS.Domain;
using NHibernate.Criterion;
using ZDSoft.LMS.Manager;


namespace ZDSoft.LMS.Component
{
    public class TransportOrderComponent : BaseComponent<TransportOrder,TransportOrderManager>,ITransportOrderService
    {
        public new void Create(TransportOrder entity)
        {
            ClientManager clientManager = new ClientManager();
            //1步
            clientManager.Create(entity.Receiver);//创建接收人
            //2步
            clientManager.Create(entity.Commisionedor);//创建发货人
            //3步
            entity.Status = TransportOrderStatus.WaitingForDispatch;
            entity.CreateDate = DateTime.Now;

            //保存订单
            manager.Create(entity);

            //4步
            //如果domain的关联属性inverse=true，就需要增加下面的内容，即手动把保存订单明细
            TransportOrderItemManager transportOrderItemManager = new TransportOrderItemManager();
            foreach (TransportOrderItem item in entity.TranportOrderItems)
            {
                item.TransportOrder = entity;
                transportOrderItemManager.Create(item);
            }
        }

                /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="txtCargoData"></param>
        /// <param name="txtStartSatationID">到站，如果为整型说明用户从数据库中选择了存在的站</param>
        /// <param name="txtDestionationID"></param>
        /// <param name="txtSendManID"></param>
        /// <param name="txtBillManID"></param>
        public void Create(TransportOrder entity, string txtCargoData,
            string txtStartSatationID,string txtStartProvinceID, string txtDestionationID,string txtDestionationProvinceID,
            string txtSendManID, string txtBillManID)
        {
            //暂时忽略创建省的功能
            #region 创建站
            if (txtStartSatationID == "")
            {
                entity.StartStation.Province = new ProvinceManager().Get(int.Parse(txtStartProvinceID));
                new StationManager().Create(entity.StartStation);
            }
            else
            {
                entity.StartStation = new StationManager().Get(int.Parse(txtStartSatationID));
            }

            if (txtDestionationID == "")
            {
                entity.DestionationStation.Province = new ProvinceManager().Get(int.Parse(txtDestionationProvinceID));
                new StationManager().Create(entity.DestionationStation);
            }
            else
            {
                entity.DestionationStation = new StationManager().Get(int.Parse(txtDestionationID));
            }
            
            #endregion

            #region 创建客户
            if (txtSendManID == "")
            {
                new ClientManager().Create(entity.Commisionedor);
            }
            else
            {
                entity.Commisionedor = new ClientManager().Get(int.Parse(txtSendManID));
            }
            if (txtBillManID == "")
            {
                new ClientManager().Create(entity.Receiver);
            }
            else
            {
                entity.Receiver = new ClientManager().Get(int.Parse(txtBillManID));
            }
            new ClientManager().Create(entity.Receiver);
            #endregion
            entity.Status = TransportOrderStatus.WaitingForDispatch;
            entity.CreateDate = DateTime.Now;
            new TransportOrderManager().Create(entity);

            string[] datas = txtCargoData.Split(',');
            for (int i = 0; i < datas.Length; i = i + 7)
            {
                TransportOrderItem item = new TransportOrderItem();
                item.CargoName = datas[i];
                item.CabinetSort = datas[i + 1];
                item.CabinetType = datas[i + 2];
                item.Counter = int.Parse(datas[i + 3]);
                item.CabinetNumber = datas[i + 4];
                item.SealedNumber = datas[i + 5];
                item.TransportFee = decimal.Parse(datas[i + 6]);

                item.TransportOrder = entity;
                new TransportOrderItemManager().Create(item);
            }
        }

        /// <summary>
        /// 更新订单信息
        /// </summary>
        /// <param name="entity"></param>
        public new void Update(TransportOrder entity)
        {
            ClientManager clientManager = new ClientManager();
            clientManager.Update(entity.Receiver);//更新收货人
            clientManager.Update(entity.Commisionedor);//更新发货人

            //更新订单
            manager.Update(entity);
            
            TransportOrderItemManager transportOrderItemManager = new TransportOrderItemManager();
            //查询订单以前的明细
            IList<TransportOrderItem> deleteItem = manager.Get(entity.ID).TranportOrderItems;
            //先删除里面所有的明细
            foreach (TransportOrderItem item in deleteItem)
            {
                if (item.ID == 0) continue;
                transportOrderItemManager.Delete(item.ID);
            }            
            //保存新的订单信息
            foreach (TransportOrderItem item in entity.TranportOrderItems)
            {
                item.TransportOrder = entity;
                transportOrderItemManager.Create(item);
            }
           
        }

        #region hql测试 create by lengyahong
        public TransportOrder GetTransportOrderTest(string startStation, string provinceName, int creatorID)
        {
            return manager.GetTransportOrderTest(startStation, provinceName, creatorID);
        }

        public IList<TransportOrder> GetTransportOrderTest(string startStation, string provinceName, string receiveName)
        {
            return manager.GetTransportOrderTest(startStation, provinceName, receiveName);
        }
        #endregion
    }
}
